<?php defined('BASEPATH') OR exit('No direct script access allowed');

class manageResolutions extends wsBackend
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('resolution_model');
    }


    public function index()
    {
        $data = array();

        $this->tpl->meta ['title'] = '壁纸尺寸管理 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '壁纸尺寸管理 ';
        $this->tpl->page_header_ = 'Resolution List';
        $this->tpl->breadcrumbs['Manage Resolution'] = 'active';


        $data['status'] = array(
            '0' => '启用',
            '1' => '未启用'
        );
        $data['status_rg'] = array(
            '0' => '启用',
            '1' => '未启用'
        );

        if ($this->input->get('resolution_name'))
            $filter['name'] = $this->input->get('resolution_name');
        if (isset($_GET['status']) && array_key_exists($this->input->get('status'), $data['status']))
            $filter['status'] = (int)$this->input->get('status');
        if (isset($_GET['status_rg']) && array_key_exists($this->input->get('status_rg'), $data['status_rg']))
            $filter['status_rg'] = (int)$this->input->get('status_rg');


        $filter['closure'] = true;
        $filter['show_disabled'] = true;
        $config['uri_segment'] = 4;
        $data['offset'] = $filter['offset'] = $this->uri->segment($config['uri_segment']);
        $config['base_url'] = preg_replace('/' . $this->config->item('url_suffix') . '$/ix', '', admin_url('manageResolutions/index'));
        $config['per_page'] = $filter['per_page'] = $this->config->item('items_per_page');
        if (!empty($_GET)) {
            $get_method = '/?' . http_build_query($this->input->get());
            $config['suffix'] = $this->config->item('url_suffix') . $get_method;
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['suffix'] = $this->config->item('url_suffix');
        }
        $this->tpl->page_header_ = $config['total_rows'] = $this->resolution_model->get_AllResolutions($filter + array('count' => true));
        $config['use_page_numbers'] = FALSE;
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        $data['resolutions'] = $this->resolution_model->get_AllResolutions($filter);


        $this->tpl->contents [] = $this->tpl->fetch('contents/manageResolutions', $data);
        $this->tpl->render();
    }

    public function delete_bulk()
    {
        $id = $this->input->post('res_id');
        if (!$id) {
            $this->session->set_flashdata('ERROR', 'Required a valid ID');
            redirect(admin_url('manageResolutions'));
        } else {
            $delete = $this->resolution_model->delete($id);
            if ($delete)
                $this->session->set_flashdata('SUCCESS', '分辨率删除成功');
        }
        redirect(admin_url('manageResolutions/index/'));
    }

    public function delete($id = '', $offset = '')
    {
        if (!$id) {
            $this->session->set_flashdata('ERROR', '分辨率ID错误');
            redirect(admin_url('manageResolutions'));
        }
        $id = (int)$id;
        $delete = $this->resolution_model->delete($id);
        if ($delete)
            $this->session->set_flashdata('SUCCESS', '分辨率删除成功');
        redirect(admin_url('manageResolutions/index/' . $offset));
    }

    public function edit($id = '', $offset = '')
    {
        $data = array();
        if (!$id) {
            $this->session->set_flashdata('ERROR', '分辨率ID错误');
            redirect(admin_url('manageResolutions'));
        }

        $data['resolution'] = $this->resolution_model->get_resolutionByID($id);

        $this->tpl->meta ['title'] = '编辑分辨率 - ' . $data['resolution']->resolution_name . ' - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '编辑分辨率';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['Manage Resolution'] = admin_url('manageResolutions');
        $this->tpl->breadcrumbs['编辑分辨率 - ' . $data['resolution']->resolution_name] = 'active';

        $this->load->library('form_validation');
        $this->form_validation->set_rules('resolution_width', 'Resolution Width', 'required|trim|numeric');
        $this->form_validation->set_rules('resolution_height', 'Resolution Height', 'required|trim|numeric');
        $this->form_validation->set_rules('resolution_group_seq_id', 'Resolution Group', 'required');

        $data['resolution_group'] = $this->resolution_model->get_groupDropDown();

        if ($this->form_validation->run() && array_key_exists($this->input->post('resolution_group_seq_id'), $data['resolution_group'])) {
            $widthHeight_check = false;
            if ($this->input->post('resolution_width') != $data['resolution']->resolution_width || $this->input->post('resolution_height') != $data['resolution']->resolution_height)
                $widthHeight_check = $this->resolution_model->get_resolutionBYWidthHeight($this->input->post('resolution_name'));
            if ($widthHeight_check) {
                $this->tpl->error[] = '分类中已经存在相同的宽度和高度 ' . $widthHeight_check->resolution_group_name;
            } else {
                $group_name = $this->input->post('resolution_name');
                if (!$group_name)
                    $group_name = $this->input->post('resolution_width') . ' x ' . $this->input->post('resolution_height');
                $update_data = array(
                    'resolution_name' => $group_name,
                    'resolution_width' => $this->input->post('resolution_width'),
                    'resolution_height' => $this->input->post('resolution_height'),
                    'resolution_group_seq_id' => $this->input->post('resolution_group_seq_id'),
                    'resolution_status' => ($this->input->post('resolution_status') ? TRUE : FALSE),
                );
                $update_id = $this->resolution_model->update($update_data, $id);
                if ($update_id) {
                    $this->session->set_flashdata('SUCCESS', $group_name . ' - 分辨率修改成功');
                    redirect(admin_url('manageResolutions/index/' . $offset));
                } else {
                    $this->tpl->error[] = 'Unable to Update the database';
                }
            }
        }


        $this->tpl->contents [] = $this->tpl->fetch('contents/manageResolutions_update', $data);
        $this->tpl->render();
    }

    public function add()
    {
        $data = array();

        $this->tpl->meta ['title'] = '添加分辨率 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '添加分辨率';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['Manage Resolution'] = admin_url('manageResolutions');
        $this->tpl->breadcrumbs['添加分辨率'] = 'active';
        $data['resolution'] = false;

        $this->load->library('form_validation');
        $this->form_validation->set_rules('resolution_width', 'Resolution Width', 'required|trim|numeric');
        $this->form_validation->set_rules('resolution_height', 'Resolution Height', 'required|trim|numeric');
        $this->form_validation->set_rules('resolution_group_seq_id', 'Resolution Group', 'required');

        $data['resolution_group'] = $this->resolution_model->get_groupDropDown();

        if ($this->form_validation->run() && array_key_exists($this->input->post('resolution_group_seq_id'), $data['resolution_group'])) {
            $widthHeight_check = $this->resolution_model->get_resolutionBYWidthHeight($this->input->post('resolution_name'));
            if ($widthHeight_check) {
                $this->tpl->error[] = '分类中已经存在相同的宽度和高度 ' . $widthHeight_check->resolution_group_name;
            } else {
                $group_name = $this->input->post('resolution_name');
                if (!$group_name)
                    $group_name = $this->input->post('resolution_width') . ' x '  . $this->input->post('resolution_height');
                $insert_data = array(
                    'resolution_name' => $group_name,
                    'resolution_width' => $this->input->post('resolution_width'),
                    'resolution_height' => $this->input->post('resolution_height'),
                    'resolution_group_seq_id' => $this->input->post('resolution_group_seq_id'),
                    'resolution_status' => ($this->input->post('resolution_status') ? TRUE : FALSE),
                );
                $insert_id = $this->resolution_model->add($insert_data);
                if ($insert_id) {
                    $this->session->set_flashdata('SUCCESS', $group_name . ' - 添加成功');
                    redirect(admin_url('manageResolutions/index'));
                } else {
                    $this->tpl->error[] = '无法更新数据库';
                }
            }
        }


        $this->tpl->contents [] = $this->tpl->fetch('contents/manageResolutions_update', $data);
        $this->tpl->render();
    }

    public function Group_list()
    {
        $data = array();

        $this->tpl->meta ['title'] = '壁纸分辨率分类管理 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '壁纸分辨率分类管理';
        $this->tpl->page_header_ = 'Resolution Group List';
        $this->tpl->breadcrumbs['Resolution'] = admin_url('manageResolutions');
        $this->tpl->breadcrumbs['Group'] = 'active';


        $data['status'] = array(
            '0' => '启用',
            '1' => '未启用'
        );


        if ($this->input->get('resolution_name'))
            $filter['name'] = $this->input->get('resolution_name');
        if (isset($_GET['status']) && array_key_exists($this->input->get('status'), $data['status']))
            $filter['status'] = (int)$this->input->get('status');


        $filter['show_disabled'] = true;
        $config['uri_segment'] = 4;
        $data['offset'] = $filter['offset'] = $this->uri->segment($config['uri_segment']);
        $config['base_url'] = preg_replace('/' . $this->config->item('url_suffix') . '$/ix', '', admin_url('manageResolutions/Group_list'));
        $config['per_page'] = $filter['per_page'] = 20;
        if (!empty($_GET)) {
            $get_method = '/?' . http_build_query($this->input->get());
            $config['suffix'] = $this->config->item('url_suffix') . $get_method;
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['suffix'] = $this->config->item('url_suffix');
        }
        $this->tpl->page_header_ = $config['total_rows'] = $this->resolution_model->get_AllResolutionsGroup($filter + array('count' => true));
        $config['use_page_numbers'] = FALSE;
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        $data['resolutions_group'] = $this->resolution_model->get_AllResolutionsGroup($filter);


        $this->tpl->contents [] = $this->tpl->fetch('contents/manageResolutionsGroup_list', $data);
        $this->tpl->render();
    }

    public function editGroup($id = '')
    {
        $data = array();
        if (!$id) {
            $this->session->set_flashdata('ERROR', 'Required a valid ID');
            redirect(admin_url('manageResolutions/Group_list'));
        } else {
            $id = (int)$id;
        }
        $this->tpl->meta ['title'] = '编辑分辨率分类 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '编辑分辨率分类';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['Manage Resolution'] = admin_url('manageResolutions');
        $this->tpl->breadcrumbs['Edit Group'] = 'active';
        $data['resolution_group'] = $this->resolution_model->get_groupByID($id);
        if (!$data['resolution_group']) {
            $this->session->set_flashdata('ERROR', $id . ' 数据库无内容');
            redirect(admin_url('manageResolutions/Group_list'));
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('resolution_group_name', 'Group Name', 'required|trim');

        if ($this->form_validation->run()) {
            $name_check = false;
            if ($this->input->post('resolution_group_name') != $data['resolution_group']->resolution_group_name)
                $name_check = $this->resolution_model->get_groupByName($this->input->post('resolution_group_name'));
            if ($name_check) {
                $this->tpl->error[] = 'Resolution Group Name Already Exists in the database';
            } else {
                $update_data = array(
                    'resolution_group_name' => $this->input->post('resolution_group_name'),
                    'resolution_group_sort' => $this->input->post('resolution_group_sort'),
                    'resolution_group_status' => ($this->input->post('resolution_group_status') ? TRUE : FALSE),
                );
                $update = $this->resolution_model->update_group($update_data, $id);
                if ($update) {
                    $this->session->set_flashdata('SUCCESS', $this->input->post('resolution_group_name') . ' - 修改完成');
                    redirect(admin_url('manageResolutions/Group_list'));
                } else {
                    $this->tpl->error[] = '无法写入数据库';
                }
            }
        }


        $this->tpl->contents [] = $this->tpl->fetch('contents/manageResolutionsGroup_update', $data);
        $this->tpl->render();
    }

    public function addGroup()
    {
        $data = array();

        $this->tpl->meta ['title'] = '添加分辨率分类 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '添加分辨率分类';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['Manage Resolution'] = admin_url('manageResolutions');
        $this->tpl->breadcrumbs['Add Group'] = 'active';
        $data['resolution_group'] = false;

        $this->load->library('form_validation');
        $this->form_validation->set_rules('resolution_group_name', 'Group Name', 'required|trim');


        if ($this->form_validation->run()) {
            $name_check = $this->resolution_model->get_groupByName($this->input->post('resolution_group_name'));
            if ($name_check) {
                $this->tpl->error[] = 'Resolution Group Name Already Exists in the database';
            } else {
                $insert_data = array(
                    'resolution_group_name' => $this->input->post('resolution_group_name'),
                    'resolution_group_sort' => $this->input->post('resolution_group_sort'),
                    'resolution_group_status' => ($this->input->post('resolution_group_status') ? TRUE : FALSE),
                );
                $insert_id = $this->resolution_model->add_group($insert_data);
                if ($insert_id) {
                    $this->session->set_flashdata('SUCCESS', $this->input->post('resolution_group_name') . ' - 添加成功');
                    redirect(admin_url('manageResolutions/Group_list'));
                } else {
                    $this->tpl->error[] = '无法写入数据库';
                }
            }
        }


        $this->tpl->contents [] = $this->tpl->fetch('contents/manageResolutionsGroup_update', $data);
        $this->tpl->render();
    }

    public function delete_Group($id = '')
    {
        if (!$id) {
            $this->session->set_flashdata('ERROR', '分类ID出错');
            redirect(admin_url('manageResolutions/Group_list'));
        }
        $id = (int)$id;
        $delete = $this->resolution_model->delete_group($id);
        if ($delete)
            $this->session->set_flashdata('SUCCESS', '分类删除成功');
        redirect(admin_url('manageResolutions/Group_list'));
    }

    public function delete_bulk_group()
    {
        $id = $this->input->post('resg_id');
        if (!$id) {
            $this->session->set_flashdata('ERROR', '分类ID出错');
            redirect(admin_url('manageResolutions/Group_list'));
        } else {
            $delete = $this->resolution_model->delete_group($id);
            if ($delete)
                $this->session->set_flashdata('SUCCESS', '分类删除成功');
        }
        redirect(admin_url('manageResolutions/Group_list'));
    }

    public function dummy_add()
    {
        $this->db->truncate('resolutions');
        $this->db->truncate('resolutions_groups');
        $list = json_decode('{"Standard 4:1":["3800x600","1024x768","1152x864","1280x960","1400x1050","1440x1080","1600x1200","1680x1260","1920x1440","2048x1536","2560x1920","2800x2100","3200x2400","4096x3072"],"Standard 5:4":["1280x1024","2560x2048","3750x3000"],"Wide 16:10":["960x600","1152x720","1280x800","1440x900","1680x1050","1920x1200","2560x1600","2880x1800","3840x2400","5120x3200"],"Wide 5:3":["800x480","1280x768"],"HD 16:9":["960x540","1024x576","1280x720","1366x768","1600x900","1920x1080","2048x1152","2400x1350","2560x1440","2880x1620","3554x1999","3840x2160"],"DSLR 3:2":["1152x768","1440x960","2000x1333"],"Mobile VGA":["240x320","480x640","320x240","640x480"],"Mobile WVGA":["240x400","480x800","400x240","800x480"],"Mobile iPhone":["320x480","480x320","640x960","960x640"],"Mobile iPad":["1024x768","768x1024"],"Mobile PSP":["480x272"],"Mobile Phone":["220x176"],"Tablet iPad 2":["1024x1024"],"Tablet Android":["1280x1280"],"Tablet iPad 3":["2048x2048"],"Dual Standard 4:3":["1600x600","2048x768","2304x864","2560x960","2800x1050","2880x1080","3200x1200","3360x1260","3840x1440","4096x1536","5120x1920"],"Dual Standard 5:4":["2560x1024","5120x2048"],"Dual Wide 16:10":["1920x600","2304x720","2560x800","2880x900","3360x1050","3840x1200","5120x1600"],"Dual Wide 5:3":["1600x480","2560x768"],"Dual HD 16:9":["1920x540","2048x576","2560x720","3200x900","3840x1080","4096x1152","4800x1350","5120x1440"]}', true);
        $i = 1;
        foreach ($list as $group_name => $resolutions) {
            $group_data = array(
                'resolution_group_name' => $group_name,
                'resolution_group_status' => 1,
                'resolution_group_sort' => $i,
            );
            $insert_id = $this->resolution_model->add_group($group_data);
            $i++;
            $insert_resolution = array();
            foreach ($resolutions as $re) {
                $explode = explode('x', $re);
                $width = trim($explode[0]);
                $height = trim($explode[1]);
                $insert_resolution[] = array(
                    'resolution_name' => $width . ' x ' . $height,
                    'resolution_group_seq_id' => $insert_id,
                    'resolution_width' => $width,
                    'resolution_height' => $height,
                    'resolution_status' => 1,
                );
            }
            if (!empty($insert_resolution))
                $this->resolution_model->add($insert_resolution, true);
        }
        redirect(admin_url('manageResolutions'));
    }
}
